package kr.saeildamil.androidforstudy2.ch33;

import java.io.IOException;

import kr.saeildamil.androidforstudy2.R;
import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.VideoView;

public class RecVideo extends Activity implements SurfaceHolder.Callback{
	MediaRecorder mRecorder = null;
	Button mStartBtn;
	Button mPlayBtn;
	String Path = "";
	boolean mIsStart = false;
	private SurfaceView mPreview;
	private SurfaceHolder mHolder;
	VideoView mVideo;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch33_recvideo);
	
		mPreview = (SurfaceView) findViewById(R.id.surface);
		mHolder = mPreview.getHolder();
		mHolder.addCallback(this);
		mVideo = (VideoView) findViewById(R.id.videoview);
		
		mStartBtn = (Button) findViewById(R.id.start);
		mPlayBtn = (Button) findViewById(R.id.play);
		
		mStartBtn.setOnClickListener(new Button.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mPreview.setVisibility(View.VISIBLE);
				mVideo.setVisibility(View.INVISIBLE);
				if(mIsStart == false){
					String sdpath = Environment.getExternalStorageDirectory().getAbsolutePath();
					Path = sdpath + "/recvideo.mp4";
					if(mRecorder == null){
						mRecorder = new MediaRecorder();
					}else{
						mRecorder.reset();
					}
					mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
					mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
					mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
					mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
					mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
					mRecorder.setOutputFile(Path);
					mRecorder.setPreviewDisplay(mHolder.getSurface());
					try {
						mRecorder.prepare();
						mRecorder.start();
					} catch (IllegalStateException e) {
						Toast.makeText(RecVideo.this, "IllegalStateException!", Toast.LENGTH_SHORT).show();
						return;
					} catch (IOException e) {
						Toast.makeText(RecVideo.this, "IOException!", Toast.LENGTH_SHORT).show();
						return;
					}
					mIsStart = true;
					mStartBtn.setText("Stop");
				}else{
					mRecorder.stop();
					mRecorder.release();
					mRecorder = null;
					mIsStart = false;
					mStartBtn.setText("Start");
				}
			}
		});
		
		mPlayBtn.setOnClickListener(new Button.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(Path.length() == 0 || mIsStart){
					Toast.makeText(RecVideo.this, "Record is first!", Toast.LENGTH_SHORT).show();
					return;
				}
				mPreview.setVisibility(View.INVISIBLE);
				mVideo.setVisibility(View.VISIBLE);
				mVideo.setVideoPath(Path);
				mVideo.start();
			}
		});
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		super.onDestroy();
		if(mRecorder != null){
			mRecorder.release();
			mRecorder = null;
		}
	}
	
}
